/*******************************************************************************
* 1. 시 스 템 : 농협보험 이미지처리 시스템
* 2. 프로그램 : DBConnection.java
* 3. 개 요 : DBConnection을 Pool로 관리한다.
* 4. 작 성 자 : 손 영휘 
* 5. 작 성 일 : 2011. 09. 30
* 6. 소스버전 : 1.0
********************************************************************************/
package com.nhim.pool;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

//import com.nhliim.common.EnvManager;
//import com.nhliim.common.Environment;
//import com.nhliim.pool.SystemEnv;
import com.nhim.utility.COMUtil;

public class DBConnection extends DBConnectionBroker
{	
	/* DataBase Connection properties */
	private static String m_sDriver			= null;
	private static String m_sUrl 			= null;
	private static String m_sUser 			= null;
	private static String m_sPassword 		= null;
	private static int m_minconnections 	= 5;
	private static int m_maxconnections 	= 30;
	
	/* Log file Path */
	private static String m_sConLogPath = null;
	
	/* DBConnection instance */
	private static DBConnection m_oInstance;
	
	private static void init()
	{
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd"); 
		Date date = new Date();
		
		m_sDriver 			= COMUtil.DB_DRIVER;
		m_sUrl				= COMUtil.DB_URL;
		m_sUser				= COMUtil.DB_USER;
		m_sPassword			= COMUtil.decrypt(COMUtil.DB_PASSWD);
		m_sConLogPath		= COMUtil.DB_LOGPATH+sdf1.format(date)+".log";
		m_minconnections	= COMUtil.DB_MINCONNECTIONS;
		m_maxconnections	= COMUtil.DB_MAXCONNECTIONS;
		
		logFileDelete(COMUtil.DB_LOGPATH);
		
		COMUtil.debug(">>> DBConnection Information <<<");
		COMUtil.debug("m_sDriver="+m_sDriver);
		COMUtil.debug("m_sUrl="+m_sUrl);
		COMUtil.debug("m_sUser="+m_sUser);
		COMUtil.debug("m_sPassword="+COMUtil.DB_PASSWD);
		COMUtil.debug("m_sConLogPath="+m_sConLogPath);
		COMUtil.debug("m_minconnections="+m_minconnections);
		COMUtil.debug("m_maxconnections="+m_maxconnections);
		COMUtil.debug("***********************************");
	}
	
	/*
	 * m_sDriver : Driver
	 * m_sUrl    : URL
	 * m_sUser   : User
	 * m_sPassword: password
	 * 2         : 최소 커넥션 갯수
	 * 7         : Dynamic pool 에서의 최대 커넥션 갯수
	 * m_sConLogPath:log file path
	 * 1.0       : 최대 커넥션 연결시간
	 * true      : log append
	 */
	private DBConnection() throws IOException
	{		
		super(m_sDriver, m_sUrl, m_sUser, m_sPassword, m_minconnections,m_maxconnections, m_sConLogPath,10000.0, true);	
	}
	
	/* DBConnection의 instance를 singleton형태로 구한다. */
	public static DBConnection getInstance()
	{	 	
		try
		{
			if(m_oInstance == null)
			{
				synchronized(DBConnection.class)
				{
					if(m_oInstance == null)
					{
						init(); 
						m_oInstance = new DBConnection();
					}
				}			
			}	
			return m_oInstance;
		}
		catch(IOException e)
		{
			COMUtil.debug("DBConnection Construction fail");
			e.printStackTrace();
			return null;
		}
	}
	
	
	/**
	 * 로그 파일을 삭제한다.
	 *
	 */
	public static void logFileDelete(String logPath){
		File beforeFile=null;
		File beforeFilePid=null;
		
		try{
			//60일 이전의 로그 파일을 삭제하기
			//Environment env = EnvManager.getEnvironment();
			int logDelStart = Integer.parseInt(COMUtil.LOG_DEL_TIME)+1;
			int logDelEnd = logDelStart+10;				
			for(int i=logDelStart; i<logDelEnd; i++){
				beforeFile= new File(logPath + getBeforeDate(i) + ".log");
				beforeFilePid= new File(logPath + getBeforeDate(i) + ".logpid");
				boolean beforeBi = beforeFile.exists();
				boolean beforeBiPid = beforeFilePid.exists();
				if(beforeBi){beforeFile.delete();}
				if(beforeBiPid){beforeFilePid.delete();}
				beforeFile=null;
				beforeFilePid=null;
			}
			//60일 이전의 로그 파일을 삭제하기			
		}catch(Exception e){
			e.printStackTrace();
		}finally{

		}
	}
	
	/**
	 * before전 날짜 구한다.
	 * @param before 
	 * @return
	 */
	public static String getBeforeDate(int before){
        Calendar calendar = Calendar.getInstance(); 
        calendar.add(Calendar.DATE, -before); 
        Date beforeWeek = calendar.getTime();     
         
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");  
        String result = formatter.format(beforeWeek); 
        return result;
	}		
	
    public static String encode(String str){
        String result = null;
        try{
            byte[] rawBytes = str.getBytes("KSC5601");
            result = new String(rawBytes, "8859_1");
        }catch(java.io.UnsupportedEncodingException e){
                       
        }
        return result;
     } 	
}
